<?php
/**
 * Created by PhpStorm.
 * User: zhlj
 * Date: 16/2/18
 * Time: 下午6:10
 */


$myfile = fopen("./ckfinder1.js", "r");
$bruce = "";
while(!feof($myfile)) {
    $bruce.= fgets($myfile) ;
}
////echo $bruce ;
fclose($myfile);
echo $s = preg_replace_callback('#(S\(\'{1}[^S\(]*\'{1}\))#',function($r){
    print_r($r);
    echo "<br/>";
//    return "";
    echo $e = mb_substr($r[1],3,mb_strlen($r[1], 'utf-8')-5, 'utf-8');echo "<br/>";
echo $t = decode($e);echo "<br/>";
//    return '"'.$t.'"';
},$bruce);
// for (var t = "", n = e.charCodeAt(0), i = 1; i < e.length; ++i)t += String.fromCharCode(e.charCodeAt(i) ^ i + n & 127);
//$s = '9^^QRsZ32#$!';
//echo decode($s);
function decode($s){
    $e = $s;
    $t = ""; $n = charCodeAt($e,0);
    for ($i = 1; $i < strlen($e); ++$i){
        $t .= fromCharCode(charCodeAt($e,$i) ^ $i + $n & 127);
    }
    return $t;
}


function charCodeAt($str, $index)
{
    return get_bianma(substr($str, $index, 1));
//    $char = mb_substr($str, $index, 1, 'UTF-8');
//
//    if (mb_check_encoding($char, 'UTF-8'))
//    {
//        $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
//        return hexdec(bin2hex($ret));
//    }
//    else
//    {
//        return null;
//    }
}


function fromCharCode($codes) {
    if (is_scalar($codes)) $codes= func_get_args();
    $str= '';
    foreach ($codes as $code) $str.= chr($code);
    return $str;
}

function get_bianma($str)//等同于js的charCodeAt()
{
    $result = array();
    for($i = 0, $l = mb_strlen($str, 'utf-8');$i < $l;++$i)
    {
        $result[] = uniord(mb_substr($str, $i, 1, 'utf-8'));
    }
    return join(",", $result);
}

function uniord($str, $from_encoding = false)
{
    $from_encoding = $from_encoding ? $from_encoding : 'UTF-8';
    if (strlen($str) == 1)
        return ord($str);
    $str = mb_convert_encoding($str, 'UCS-4BE', $from_encoding);
    $tmp = unpack('N', $str);
    return $tmp[1];
}